package com.andy.note.main.generics;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * LRUCache 一段泛型程序来实现LRU缓存
 * 参考文章：
 * https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247483810&idx=6&sn=8a3d6d0d4ae64475f52f65f1b26ffc63&chksm=ebd63e8edca1b79802ffca601769dafed61d36d7f4798b327831bdba8f6a72f12f39c806dd95&scene=21#wechat_redirect
 * @author Andy
 *
 */
@SuppressWarnings({ "unchecked", "serial", "rawtypes" })
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
	  private int cacheSize;
	 
	  public LRUCache(int cacheSize) {
	    super(16, (float) 0.75, true);
	    this.cacheSize = cacheSize;
	  }
	 
	  protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
	    return size() >= cacheSize;
	  }
	  public static void main(String[] args) {
		LRUCache lc = new LRUCache(2);
		lc.put("a", "a");
		lc.put("b", "b");
		lc.put("c", "c");
		lc.put("d", "d");
		System.out.println(lc.toString());
	}
	}
